<template>
    <div class="client-meal-config-container">
        <h2 class="title">查看客户膳食配置</h2>

        <!-- 查询区域 -->
        <el-form :inline="true" :model="searchForm" class="search-form">
            <el-form-item label="客户名：">
                <el-input v-model="searchForm.name" placeholder="请输入客户名" clearable />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="fetchData">查询</el-button>
                <el-button @click="resetSearch">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 表格展示 -->
        <el-table :data="tableData" border style="width: 100%; background: #f5f7fa"
            :header-cell-style="{ background: '#f9fafc', color: '#333', textAlign: 'center' }"
            :cell-style="{ textAlign: 'center' }">
            <el-table-column label="客户名称" width="150">
                <template #default="scope">{{ scope.row.client.name }}</template>
            </el-table-column>
            <el-table-column label="性别" width="100">
                <template #default="scope">{{ scope.row.client.gender }}</template>
            </el-table-column>
            <el-table-column label="年龄" width="80">
                <template #default="scope">{{ scope.row.client.age }}</template>
            </el-table-column>
            <el-table-column label="膳食类型" width="100">
                <template #default="scope">{{ scope.row.mealConfig.diettype }}</template>
            </el-table-column>
            <el-table-column label="盐分" width="100">
                <template #default="scope">{{ scope.row.mealConfig.saltId }}</template>
            </el-table-column>
            <el-table-column label="油脂" width="100">
                <template #default="scope">{{ scope.row.mealConfig.oil }}</template>
            </el-table-column>
            <el-table-column label="糖分" width="100">
                <template #default="scope">{{ scope.row.mealConfig.sugar }}</template>
            </el-table-column>
            <el-table-column label="描述" min-width="150">
                <template #default="scope">{{ scope.row.mealConfig.description }}</template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-bar">
            <el-pagination v-model:current-page="pagination.cur" v-model:page-size="pagination.size"
                :total="pagination.total" :page-sizes="[5, 10, 20]" layout="total, sizes, prev, pager, next, jumper"
                @current-change="fetchData" @size-change="handleSizeChange" />
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import {inject} from 'vue'
const axios = inject("axios")
import { ElMessage } from 'element-plus'

const searchForm = ref({ name: '' })

const tableData = ref([])
const pagination = ref({
    cur: 1,
    size: 10,
    total: 0
})

const fetchData = async () => {
    try {
        const res = await axios.get('/mealConfig/pageClientAndConfig', {
            params: {
                cur: pagination.value.cur,
                number: pagination.value.size,
                name: searchForm.value.name
            }
        })
        if (res.data.status === 200) {
            tableData.value = res.data.data || []
            pagination.value.total = res.data.total
        } else {
            throw new Error(res.data.msg || '加载失败')
        }
    } catch (err) {
        ElMessage.error('查询失败：' + err.message)
    }
}

const handleSizeChange = (size) => {
    pagination.value.size = size
    pagination.value.cur = 1
    fetchData()
}

const resetSearch = () => {
    searchForm.value.name = ''
    pagination.value.cur = 1
    fetchData()
}

onMounted(fetchData)
</script>

<style scoped>
.client-meal-config-container {
    padding: 0 3%;
    background: #fff;
}

.title {
    font-size: 27.2px;
    font-weight: bold;
    margin-top: 0;
    margin-bottom: 20px;
}

.search-form {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 12px 24px;
    margin-bottom: 20px;
}

.pagination-bar {
    width: 100%;
    display: flex;
    justify-content: flex-end;
    margin-top: 2%;
    margin-bottom: 2%;
}
</style>
